<?php
namespace app\admin\controller;
use think\Controller;

class Login extends Controller
{
    public function index()
    {
    	if (request()->isAjax()) {
    		$user_name = input("post.user_name");
    		$pass_word = input("post.pass_word");
    		$verify_code = input("post.verify_code");
    		// captcha_check($value, $id = "", $config = [])
    		if (captcha_check($verify_code)) {
                // session('ad_name',$user_name);
                // $array = ["status" => 1,"message"=>"登录成功"];
                $where["user_name"] = $user_name;
                $where["pass_word"] = md5(config("afs.pass_prefix").md5($pass_word));
                $user = db("ad_users")->where($where)->find();
                if ($user) {
                    session('ad_name',$user["user_name"]);
                    session('ad_id',$user["id"]);
                    session('ad_group_id',$user["group_id"]);
                    $update["login_time"] = time();
                    $update["login_ip"] = get_ip();
                    $update["last_login_time"] = $user["login_time"];
                    $update["last_login_ip"] = $user["login_ip"];
                    db("ad_users")->where("id = ".$user["id"])->update($update);
                    $array = ["status" => 1,"message"=>"登录成功"];
                }else{
                    $array = ["status" => 0,"message"=>"用户名或密码不正确"]; 
                }
    		}else{
    			$array = ["status" => 0,"message"=>"验证码不正确"];
    		}
    		return json($array);
    	}
        session(null);
        return $this->fetch();
    }

    public function logout(){
        session(null);
        $this->redirect('/admin/index.html');
    }
}
